package com.gopay.common.util;

import org.apache.log4j.PatternLayout;
import org.apache.log4j.spi.LoggingEvent;
/**
 * <p>背景：在交易发生阶段，签名验签的明文中含有商户识别码，会直接打印到日志，存在安全隐患，需把商户识别码做隐藏处理。
 * 在此之前，采用{@link MyFileAppender}进行识别码做隐藏处理
 * <p>目的：为解决{@link MyFileAppender}不能做日终日志切分所存在的缺陷
 * <p>说明：隐藏识别码的逻辑重新定义
 * @GraffitiPatternLayout.java
 * @author lizheng
 * @2014-12-4 下午2:52:49  www.gopay.com.cn Inc.All rights reserved.
 */
public class GraffitiPatternLayout  extends PatternLayout {
    private static String  FILTERSTR = "VerficationCode=[";
//    private static String  REPLACEFILTERSTR = "VerficationCode=\\[";
    private static String  OBJ_REG_EXP = "VerficationCode=\\[\\w+\\]";
    private static String  OBJ_REP_EXP = "VerficationCode=[**Graffiti**]";
    @Override
    public String format(LoggingEvent event) {
//        String message= super.format(event);
        if(event.getMessage() == null) {
            return super.format(event);
        }

        String message =  event.getMessage().toString();
        int start =message.indexOf(FILTERSTR);
//        String rep ="";
//        String x ="";
//        if(start!=-1){
//            String str =  message.substring(start);
//            int end = str.indexOf("]");
//            if(end!=-1){
//                str = str.substring(0,end);
//                rep = str.replaceAll(REPLACEFILTERSTR, "");
//                int length = rep.length();
//
//                for (int i=0;i<length;i++) {
//                    x=x+"*";
//                }
//                rep = REPLACEFILTERSTR+rep;
//
//                x = FILTERSTR+x;
//            }
//            return super.format(event).replaceAll(rep, x);
        if(start!=-1){
            return super.format(event).replaceAll(OBJ_REG_EXP, OBJ_REP_EXP);
        }else{
            return super.format(event);
        }
        
    }

}
